package com.bening.smsapp.bean;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;

import com.bening.smsapp.bootstrap.BootConfigParameters;
import com.bening.smsapp.constant.SmsAppConstant;

public class RawBean {
	static BootConfigParameters bootParams = BootConfigParameters.getInstance();
	List rootTagList = new ArrayList();
	List valueTagList = new ArrayList();
	List valueList = new ArrayList();
	List rootValueList = new ArrayList();
	final Logger logger = Logger.getLogger( RawBean.class );
	
	public void setValueTagNum(int valueTagNum) {
		valueTagList.add(Integer.valueOf(valueTagNum));
	}

	public void setTagnum(int tagnum) {
		rootTagList.add(Integer.valueOf(tagnum));
	}

	public void setValueList(String satu) {
		//System.out.println(">>"+satu);
		valueList.add(satu);
	}
	public void setRootValueList(String Nol) {
		//System.out.println(">"+Nol);
		rootValueList.add(Nol);
	}
	public String getSize() {
		StringBuffer sb = new StringBuffer();
		sb.append("valueList :"+valueList.size()+'\n');
		sb.append("rootValueList :"+rootValueList.size()+'\n');
		sb.append("tagList :"+rootTagList.size()+'\n');
		sb.append("valueTagList :"+valueTagList.size()+'\n');
		return sb.toString();
	}
	
	public Asn1Bean buildAsn1Bean() {
		Asn1Bean bean = new Asn1Bean();
		int childPopCounter = 0;
		for(int i=0;i<rootTagList.size();i++) {
			
			int rootTag = (Integer)(rootTagList.get(i)); 
			//BeanUtils.setProperty(bean, arg1, arg2)
			String method = bootParams.getXmlProperties().getProperty(SmsAppConstant.PROPERTIES_TAG+rootTag).trim();
			//System.out.println(method+" "+i);
			String childLen = bootParams.getXmlProperties().getProperty(SmsAppConstant.PROPERTIES_TAG+rootTag+SmsAppConstant.CHILD);
			try {
				//System.out.println("invoking "+ method+String.valueOf(rootValueList.get(i)));
				BeanUtils.setProperty(bean, method, String.valueOf(rootValueList.get(i)));
				if(childLen!=null) {
					int child = Integer.valueOf(childLen.trim()).intValue();
					for(int j=0;j<child;j++) {
						childPopCounter++;
						//System.out.println(SmsAppConstant.PROPERTIES_TAG+rootTag+SmsAppConstant.CHILD_+childPopCounter);
						method = bootParams.getXmlProperties().getProperty(SmsAppConstant.PROPERTIES_TAG+rootTag+SmsAppConstant.CHILD_+(childPopCounter-1)).trim();
						//System.out.println("invoking "+ method+"-"+String.valueOf(valueList.get(childPopCounter-1)));						
						if(method!=null)
							BeanUtils.setProperty(bean, method, String.valueOf(valueList.get(childPopCounter-1)));
						
					}
				}
			}catch(InvocationTargetException ite) {
				
				logger.error(ite);
			}catch(IllegalAccessException iae) {
				logger.error(iae);
			}catch(NullPointerException n) {
				//logger.error(n);
			}
			
		}
		// normalizing
		bean.setOrigAddressMsisdnNoDecode(normalizePrefix(trimZeroPrefix(bean.getOrigAddressMsisdn())));
		bean.setRecipAddressMsisdnNoDecode(normalizePrefix(trimZeroPrefix(bean.getRecipAddressMsisdn())));		
		// flagging
		bean.setClgFlag(flagging(bean.getOrigAddressMsisdn()));
		bean.setCldFlag(flagging(bean.getRecipAddressMsisdn()));

		return bean;
	}

    private static String flagging(String input) {
    	String result = "";
    	if(input==null||"".equals(input)) return "";
    	int len = input.length();
    	if(len<=6) {
    		result = SmsAppConstant.FLAG_MACHINE;
    	}else {
    		String prefix = input.substring(0,5);
    		result = bootParams.getFlaggingRuleProperties().getProperty(prefix);
    		if(result == null) {
    			result = SmsAppConstant.FLAG_EXT_OPERS;
    		}
    	}
    	return result;
    }
    private static String normalizePrefix(String input) {
    	String result = "";
    	if(input==null||"".equals(input)) return "";
    	else if(input.length()<=2) return "";
    	
    	if(!input.substring(0,2).equals("62")) {
    		if(input.length()<=6) {
    			result = input;
    		}
    		else {
    			String temp = "62"+input.substring(0,3);
    			if(bootParams.getFlaggingRuleProperties().getProperty(temp)!=null) {
    				result = "62"+input;
    			}
    			else {
    				result = input;
    			}
    		}
    	}
    	else {
    		result = input;
    	}
    	
    	return result;
    }
    private static String trimZeroPrefix(String input) {
    	if(input==null ) return "0";
    	else if("".equals(input.trim())) return "0";
    	else if("0".equals(input)) return "0";
    	else 
    	return input.replaceAll("^0*", "");
    }
    private static String trimSpaces(String input) {
    	if(input==null ) return "";
    	else 
    	return input.replaceAll("^\\s*", "");
    }
    private static Date toDateString(String date) {
    	String dateStr = date.substring(0,date.length()-4);
    	SimpleDateFormat sdf = new SimpleDateFormat("yyMMddhhmmss");
    	try {
    		return sdf.parse(dateStr);
    	}catch(ParseException p) {
    		return null;
    	}
    }

}
